package cn.iocoder.yudao.module.ride.controller.admin.ruledict;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ride.controller.admin.ruledict.vo.*;
import cn.iocoder.yudao.module.ride.dal.dataobject.ruledict.RuleDictDO;
import cn.iocoder.yudao.module.ride.service.ruledict.RuleDictService;

@Tag(name = "管理后台 - 规则字典")
@RestController
@RequestMapping("/ride/rule-dict")
@Validated
public class RuleDictController {

    @Resource
    private RuleDictService ruleDictService;

    @PostMapping("/create")
    @Operation(summary = "创建规则字典")
    @PreAuthorize("@ss.hasPermission('ride:rule-dict:create')")
    public CommonResult<Integer> createRuleDict(@Valid @RequestBody RuleDictSaveReqVO createReqVO) {
        return success(ruleDictService.createRuleDict(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新规则字典")
    @PreAuthorize("@ss.hasPermission('ride:rule-dict:update')")
    public CommonResult<Boolean> updateRuleDict(@Valid @RequestBody RuleDictSaveReqVO updateReqVO) {
        ruleDictService.updateRuleDict(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除规则字典")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ride:rule-dict:delete')")
    public CommonResult<Boolean> deleteRuleDict(@RequestParam("id") Integer id) {
        ruleDictService.deleteRuleDict(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除规则字典")
                @PreAuthorize("@ss.hasPermission('ride:rule-dict:delete')")
    public CommonResult<Boolean> deleteRuleDictList(@RequestParam("ids") List<Integer> ids) {
        ruleDictService.deleteRuleDictListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得规则字典")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ride:rule-dict:query')")
    public CommonResult<RuleDictRespVO> getRuleDict(@RequestParam("id") Integer id) {
        RuleDictDO ruleDict = ruleDictService.getRuleDict(id);
        return success(BeanUtils.toBean(ruleDict, RuleDictRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得规则字典分页")
    @PreAuthorize("@ss.hasPermission('ride:rule-dict:query')")
    public CommonResult<PageResult<RuleDictRespVO>> getRuleDictPage(@Valid RuleDictPageReqVO pageReqVO) {
        PageResult<RuleDictDO> pageResult = ruleDictService.getRuleDictPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RuleDictRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出规则字典 Excel")
    @PreAuthorize("@ss.hasPermission('ride:rule-dict:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRuleDictExcel(@Valid RuleDictPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RuleDictDO> list = ruleDictService.getRuleDictPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "规则字典.xls", "数据", RuleDictRespVO.class,
                        BeanUtils.toBean(list, RuleDictRespVO.class));
    }

}